package kr.co.mstyleshop.android.util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.content.Context;
import android.widget.Toast;

public class HttpUtils {
	public static String cookieId;

	public static String get(String path, Context context) {
		
		boolean isConnected = NetworkUtil.isConnected(context);
		if(!isConnected) {
			Toast.makeText(context, "네트워크 상태를 확인하세요!", Toast.LENGTH_SHORT).show();
			return null;
		}
		
		StringBuilder sb = new StringBuilder();
		
		HttpGet request = new HttpGet(path);
		if(cookieId != null)
			request.setHeader("Cookie", cookieId);

		HttpClient client = new DefaultHttpClient();
	
		
		try {
			HttpResponse response = client.execute(request);
			/*Header[] headers = response.getAllHeaders(); 
			String txt = "1:::::";
	        for(Header header : headers) { 
	        	txt += header.getName() + " : " + header.getValue() + "\n"; 
	        	Log.d("HttpUtils", txt);
	        } */ 

			if (200 != response.getStatusLine().getStatusCode()) {
				return null;
			}

			InputStream is = response.getEntity().getContent();
			BufferedReader reader = new BufferedReader(
					new InputStreamReader(is));

			String str = null;
			while ((str = reader.readLine()) != null) {
				sb.append(str);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return sb.toString();
	}
}
